﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;


public partial class Nastan : System.Web.UI.Page
{
    bool klik;
    string conn = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;

    protected void Page_Load(object sender, EventArgs e)
    {        
        if (!IsPostBack)
        {
            ViewState["br"] = 0;
            List<Byte[]> lista = new List<Byte[]>();
            ViewState["lista"] = lista;
            ValidacijaSlika.Visible = false;
            klik = false;
        }
        kalendar.MaxValidDate = DateTime.Today;
        kopcinja();
    }
    protected void kopcinja()
    {
        ((ImageButton)Master.FindControl("imbMakedonija")).ImageUrl = "mk-c.gif";
        ((ImageButton)Master.FindControl("imbPocetna")).ImageUrl = "pocetna-c.gif";
        ((ImageButton)Master.FindControl("imbSvet")).ImageUrl = "svet-c.gif";
        ((ImageButton)Master.FindControl("imbSport")).ImageUrl = "sport-c.gif";
        ((ImageButton)Master.FindControl("imbNauka")).ImageUrl = "nauka-c.gif";
        ((ImageButton)Master.FindControl("imbKultura")).ImageUrl = "kultura-c.gif";
        ((ImageButton)Master.FindControl("imbSouBiznis")).ImageUrl = "sou-biz-c.gif";
    }
    protected void btnVideo_Click(object sender, EventArgs e)
    {
        btnVideo.Enabled = false;        
            Byte []clipByte;
            if (fuVideo.HasFile && fuVideo.PostedFile != null)
            {                
                //To create a PostedFile
                HttpPostedFile File1 = fuVideo.PostedFile;
                //Create byte Array with file len
                clipByte = new Byte[File1.ContentLength];
                //force the control to load data in array
                File1.InputStream.Read(clipByte, 0, File1.ContentLength);
                //Session["video"] = clipByte;
                int k = Convert.ToInt32(zemiId()) + 1;
                SqlConnection connection = new SqlConnection(conn);
                try
                {
                    connection.Open();
                    string sql_video = "INSERT INTO video VALUES(@nastan_id, @video)";
                    SqlCommand cmd3 = new SqlCommand(sql_video, connection);
                    cmd3.Parameters.AddWithValue("@nastan_id", k);
                    Byte[] video = clipByte;
                    cmd3.Parameters.AddWithValue("@video", video);
                    cmd3.ExecuteNonQuery();
                    Label1.Text = "Успешно закачивте видео!";
                }
                catch (Exception err)
                {
                    Label1.Text = err.Message;
                }
                finally
                {
                    connection.Close();
                }
            }                                   
    }    
    protected void btnSlika_Click(object sender, EventArgs e)
    {
        if (fuSlika.HasFile)
        {
            blSliki.Items.Add(fuSlika.FileName);
            ViewState["br"] = Convert.ToInt32(ViewState["br"]) + 1;
            ValidacijaSlika.Visible = false; 
        }
        else
        {           
            ValidacijaSlika.Visible = true;           
            return;
        }
        
            Byte[] imgByte = null;
            if (fuSlika.HasFile && fuSlika.PostedFile != null)
            {
                //To create a PostedFile
                HttpPostedFile File = fuSlika.PostedFile;
                //Create byte Array with file len
                imgByte = new Byte[File.ContentLength];
                //force the control to load data in array
                File.InputStream.Read(imgByte, 0, File.ContentLength);                
            }            
            List<Byte[]> lista = (List<Byte[]>)ViewState["lista"];
            lista.Add(imgByte);
            ViewState["lista"] = lista;            
    }    
    protected void btnPotvrda_Click(object sender, EventArgs e)
    {
        if (blSliki.Items.Count == 0)
        {
            ValidacijaSlika.Visible = true;
            return;
        }
        else ValidacijaSlika.Visible = false;

        SqlConnection connection = new SqlConnection(conn);

        string sql_nastan = "INSERT INTO nastan VALUES(@naslov,@rang,@opis,@datum,@lokacija,@ime,@ime_kategorija)";
        SqlCommand cmd2 = new SqlCommand(sql_nastan, connection);
        cmd2.Parameters.AddWithValue("@naslov", txtNaslov.Text);
        cmd2.Parameters.AddWithValue("@rang", 0);
        cmd2.Parameters.AddWithValue("@opis", txtOpis.Text);

        if (kalendar.SelectedDate>DateTime.Today)
            cmd2.Parameters.AddWithValue("@datum", DateTime.Today);
        else
            cmd2.Parameters.AddWithValue("@datum", kalendar.SelectedDate);

        cmd2.Parameters.AddWithValue("@lokacija", txtLokacija.Text);
        string ime = Session["korisnik"].ToString();
        cmd2.Parameters.AddWithValue("@ime", ime);
        cmd2.Parameters.AddWithValue("@ime_kategorija", ddlKategorija.SelectedItem.Text);

        try
        {
            connection.Open();
            cmd2.ExecuteNonQuery();
            Label1.Text = "Успешно закачивте слика!";

            string nastan_id = "SELECT MAX(nastan_id) as posleden FROM nastan ";
            SqlCommand cmd1 = new SqlCommand(nastan_id, connection);
            
            SqlDataReader citac = cmd1.ExecuteReader();
            
            int br = Convert.ToInt32(ViewState["br"]);
            citac.Read();
            int k = citac.GetInt32(0);
            ViewState["id"] = k;
            citac.Close();
            for (int i = 0; i < br; i++)
            {
                string sql = "INSERT INTO slika VALUES(@nastan_id, @eimg)";
                SqlCommand cmd = new SqlCommand(sql, connection);
                // dodavano
                List<Byte[]> lista = (List<Byte[]>)ViewState["lista"];
                cmd.Parameters.AddWithValue("@nastan_id", k);
                cmd.Parameters.AddWithValue("@eimg", lista.ElementAt(i));
                cmd.ExecuteNonQuery();
                Label1.Text = "Успешно креиравте настан!";
            }           

            Response.Redirect("Pregled.aspx?id=" + k);
        }
        catch (Exception err)
        {
            Label1.Text = err.Message;
        }
        finally
        {
            connection.Close();
        }        
    }
    protected string zemiId()
    {
        string id = "";
        Baza baza = new Baza();
        string sqlString = "SELECT MAX(nastan_id) as posleden FROM nastan ";
        DataSet ds = baza.Zemi(sqlString, "id");
        id = ds.Tables["id"].Rows[0][0].ToString();
        return id;
    }
    protected void kalendar_SelectionChanged1(object sender, EventArgs e)
    {
        klik = true;
    }
}